package com.hsmw.api.service;

import com.aliyun.dyplsapi20170525.Client;
import com.aliyun.tea.*;
import com.aliyun.dyplsapi20170525.*;
import com.aliyun.dyplsapi20170525.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import com.hsmw.api.vo.XuniTelNumVO;
import com.htn.common.core.constant.CommonConstants;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import static com.htn.common.core.constant.CommonConstants.*;

public class IACasClientUtils {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint =ALIYUN_URL;
        return new Client(config);
    }

    public static XuniTelNumVO bindAxb(XuniTelNumVO param)  {
        try {
            com.aliyun.dyplsapi20170525.Client client = IACasClientUtils.createClient(CommonConstants.ACCESSKEYID, CommonConstants.ACCESSKEYDECRET);
            BindAxbRequest bindAxbRequest = new BindAxbRequest();
            //
            String phoneA =param.getPhoneA();
            String phoneB = param.getPhoneB();
            System.out.println(phoneA+"---"+phoneB);
            bindAxbRequest.setPhoneNoA(phoneA);
            bindAxbRequest.setPhoneNoB(phoneB);
            bindAxbRequest.setExpiration(getAfterFiveMinutes());
            bindAxbRequest.setPoolKey(POOLKEY_1);
            // 复制代码运行请自行打印 API 的返回值
            BindAxbResponse response = client.bindAxb(bindAxbRequest);
            System.out.println(response.getBody().getMessage());
            BindAxbResponseBody.BindAxbResponseBodySecretBindDTO dto = (response.getBody().getSecretBindDTO());
            if(null!=dto){
                System.out.println("号码A="+phoneA +"和号码B="+phoneB+"产生的虚拟号码是 =="+ dto.getSecretNo());
                XuniTelNumVO vo =new XuniTelNumVO();
                vo.setSecretNo(dto.getSecretNo());
                vo.setSubsId(dto.getSubsId());
                System.out.println(vo.toString());
                return vo;
            }else{
                XuniTelNumVO vo =new XuniTelNumVO();
                vo.setResult("isv.NO_AVAILABLE_NUMBER");
                return vo;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    public static void unBindAxb(XuniTelNumVO vo)  {
        try {
            com.aliyun.dyplsapi20170525.Client client = IACasClientUtils.createClient(CommonConstants.ACCESSKEYID, CommonConstants.ACCESSKEYDECRET);
            UnbindSubscriptionRequest unbindSubscriptionRequest = new UnbindSubscriptionRequest();
            unbindSubscriptionRequest.setSubsId(vo.getSubsId());
            unbindSubscriptionRequest.setSecretNo(vo.getSecretNo());
            unbindSubscriptionRequest.setPoolKey(vo.getPoolKey());
            UnbindSubscriptionResponse response =client.unbindSubscription(unbindSubscriptionRequest);
            UnbindSubscriptionResponseBody body = response.getBody();
            System.out.println(body.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static String  getAfterFiveMinutes(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, 1);// 今天+5分钟
        Date finishTime = c.getTime();
        return  format.format(finishTime);
    }


    public static void main(String[] args) throws Exception {
//18500200183 工长
        //13212040676 司机1
        //13520391225 司机2
        //18146512269 司机3
        XuniTelNumVO vo = new XuniTelNumVO();
        vo.setPhoneA("13520391228");
        vo.setPhoneB("18698005776");

       bindAxb(vo);





    }
}
